using System;
using Scenario.WorkflowEngine.DataModel;

namespace Scenario.WorkflowEngine.DataModel.WorkflowProcesses
{
	//*********************************************************************
	//
	// Transition Class
	//
	// The XmlMessage class represents, in a workflow template, how to
	// jump information flow from an activity to an other ativity.
	// Every WF contains at least one transition from start activity to end.
	//
	//*********************************************************************

	public class XMLMessage : bOS.XML.Data.XMLObject 
	{
		public const string MAIN_NODE	= "MessageFlow";

		private const string FROM_ATTRIBUTE	= "From";
		private const string TO_ATTRIBUTE	= "To";
        private const string ID_ATTRIBUTE = "Id";

		private string sFrom;
		private string sTo;
        private string sId;

        private Common.XMLExtendedAttributes vExtendedAttributes;

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Width simple Property **/
		public XMLMessage()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLMessage(System.Xml.XmlNode node)
		{
			this.Parse(node);
		}

		/** PROPERTIES **/

        public string Id
        {
            get { return sId; }
            set { sId = value; }
        }

		public string From 
		{
			get { return sFrom; }
			set { sFrom = value; }
		}

		public string To 
		{
			get { return sTo; }
			set { sTo = value; }
		}

        public Common.XMLExtendedAttributes XMLExtendedAttributes
        {
            get { return vExtendedAttributes; }
            set { vExtendedAttributes = value; }
        }

		/** INSTANCE METHODS **/

		/** Inizializing method **/
		override public void Init()
		{
			sFrom= string.Empty;
			sTo= string.Empty;
            sId = string.Empty;
            vExtendedAttributes = new Common.XMLExtendedAttributes();
		}

		/** Parsering method for filling the transition **/
		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			this.Init();

			System.Xml.XmlNode node;

			/** find the attributes situated on the root tag **/
			for ( int i=0; i<node2Parse.Attributes.Count;i++)
			{
				node = node2Parse.Attributes.Item(i);

				switch ( node.Name )
				{
                    case XMLMessage.ID_ATTRIBUTE:
                        sId = node.InnerText;
                        break;

					case XMLMessage.FROM_ATTRIBUTE:
						sFrom = node.InnerText;
						break;
                    case XMLMessage.TO_ATTRIBUTE:
						sTo = node.InnerText;


						break;
				}
			}

			/** find the sub tags witch contains values **/
			for ( int i=0; i<node2Parse.ChildNodes.Count; i++)
			{
				node = node2Parse.ChildNodes[i];

				switch ( node.Name )
				{
                    case Common.XMLExtendedAttributes.MAIN_NODE:
                        vExtendedAttributes.Parse(node);
                        break;
				}
			}
		}

		override public bOS.XML.XMLNode GetXMLNode()
		{
			bOS.XML.XMLNode xmlNode = new bOS.XML.XMLNode(XMLMessage.MAIN_NODE);
            xmlNode.AddAttribute(new bOS.XML.XMLAttribute(XMLMessage.ID_ATTRIBUTE, sId));
            xmlNode.AddAttribute(new bOS.XML.XMLAttribute(XMLMessage.FROM_ATTRIBUTE, sFrom));
            xmlNode.AddAttribute(new bOS.XML.XMLAttribute(XMLMessage.TO_ATTRIBUTE, sTo));

            if (vExtendedAttributes.Count > 0)
			    xmlNode.AddNode ( vExtendedAttributes.GetXMLNode () );
		
			return xmlNode;
		}
	}
}